// PluginManager.cpp: implementation of the CPluginManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansieditor.h"
#include "PluginManager.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPluginManager::CPluginManager()
{
	gpPluginManager = this;
}

CPluginManager::~CPluginManager()
{
	int nCount = m_arrItemBitmap.GetSize();
	for(int n = 0; n < nCount; n++)
	{
		delete m_arrItemBitmap.GetAt(n);
	}
	gpPluginManager = NULL;
}

void CPluginManager::LoadPlugins()
{
	WIN32_FIND_DATA fd = { 0 };
	CString &sAppPath = gpApp->GetAppPath();
	HANDLE hFind = FindFirstFile(sAppPath + "\\plugin\\*.dll", &fd);
	if(hFind == INVALID_HANDLE_VALUE)
	{
		return;
	}
	do
	{
		CPlugin *pPlugin = new CPlugin(fd.cFileName);
		if(pPlugin->Load())
		{
			CString sFind;
			sFind.Format("[%s]", fd.cFileName);
			pPlugin->Enable(gpApp->m_options.sDisabledPlugins.Find(sFind) == -1);
			int nIndex = m_arrPlugin.Add(pPlugin);
			UNUSED_ALWAYS(nIndex);
		}
		else
		{
			delete pPlugin;
		}
	} while(FindNextFile(hFind, &fd));
	FindClose(hFind);

	LoadPluginUI();
}

void CPluginManager::ReleasePlugins()
{
	int nCount = m_arrPlugin.GetSize();
	for(int n = 0; n < nCount; n++)
	{
		delete m_arrPlugin.GetAt(n);
	}
}

void CPluginManager::OnPluginCommand(UINT nID)
{
	m_arrPlugin.GetAt(nID - IDM_PLUGIN_BASE)->OnCommand();
}

CPlugin* CPluginManager::GetPluginByCommandID(UINT nID)
{
	if((nID >= IDM_PLUGIN_BASE) && (nID <= IDM_PLUGIN_MAX))
	{
		return m_arrPlugin.GetAt(nID - IDM_PLUGIN_BASE);
	}
	return NULL;
}



void CPluginManager::LoadPluginUI()
{
	int nCount = m_arrPlugin.GetSize();
	if(nCount == 0)
	{
		return;
	}
//	m_il.Create(16, 16, ILC_COLOR24|ILC_MASK, 0, 1);
	CMenu *pMenu = gpFrame->GetMenu();
	pMenu = pMenu->GetSubMenu(5);
	pMenu->AppendMenu(MF_SEPARATOR);

	BOOL bTbSepAdded = FALSE;
	for(int n = 0; n < nCount; n++)
	{
		CPlugin *pPlugin = m_arrPlugin.GetAt(n);
		CString sMenuName = pPlugin->GetMenuName();
		if(sMenuName.IsEmpty())
		{
			continue;	// no ui
		}

		IPicture *pPicture = NULL;
		if(pPlugin->GetMenuIcon(&pPicture))
		{
			HICON hIcon = 0;
			pPicture->get_Handle((OLE_HANDLE*)&hIcon);
			
//			int nPos = m_il.Add(hIcon);
			int nPos = gpFrame->m_ilToolBar.Add(hIcon);
			pPlugin->m_iBitmap = (BYTE)nPos;

			CBitmapMenu::CItemBitmap *pib = new CBitmapMenu::CItemBitmap(IDM_PLUGIN_BASE + n, &gpFrame->m_ilToolBar, nPos);
			m_arrItemBitmap.Add(pib);
			gpFrame->m_menu.AddBitmaps(pib, 1);
		
			if(!bTbSepAdded)
			{
				TBBUTTON tbsep = { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0};
				gpFrame->m_wndToolBar.GetToolBarCtrl().AddButtons(1, &tbsep);
				bTbSepAdded = TRUE;
			}
			
			TBBUTTON button = { nPos, IDM_PLUGIN_BASE + n, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0};
			gpFrame->m_wndToolBar.GetToolBarCtrl().AddButtons(1, &button);
			garrTbButton.Add(button);

			pPicture->Release();
		}

		pMenu->AppendMenu(MF_STRING, IDM_PLUGIN_BASE + n, sMenuName);
	}
}

CPlugin* CPluginManager::GetPluginByID(UINT nID)
{
	return m_arrPlugin.GetAt(nID - IDM_PLUGIN_BASE);
}

int CPluginManager::GetPluginCount()
{
	return m_arrPlugin.GetSize();
}
